package com.ita.acmsystem.merchant.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ita.acmsystem.merchant.dao.IFoodTypesDao;
import com.ita.acmsystem.merchant.entity.FoodTypes;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.util.DBUtil;

public class FoodTypesDaoImpl implements IFoodTypesDao {

	@Override
	public FoodTypes query(String typeId) {
		FoodTypes foodTypes = null;

		String sql = "select * from food_types where type_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, typeId);
			rs = ps.executeQuery();

			if (rs.next()) {
				foodTypes = new FoodTypes();
				foodTypes.setTypeId(rs.getString(1));
				foodTypes.setTypeName(rs.getString(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return foodTypes;
	}

	@Override
	public List<FoodTypes> queryAll() {
		List<FoodTypes> list = new ArrayList<FoodTypes>();
		FoodTypes foodTypes = null;

		String sql = "select * from food_types";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				foodTypes = new FoodTypes();
				foodTypes.setTypeId(rs.getString(1));
				foodTypes.setTypeName(rs.getString(2));
				list.add(foodTypes);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return list;
	}

	@Override
	public FoodTypes persist(FoodTypes foodTypes) {

		FoodTypes result = null;

		String sql = "insert into food_types values(?,?)";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foodTypes.getTypeId());
			ps.setString(2, foodTypes.getTypeName());
			rs = ps.executeQuery();

			result = foodTypes;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

	@Override
	public boolean update(FoodTypes foodTypes) {

		boolean result = false;

		String sql = "update food_types set type_name=? where type_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, foodTypes.getTypeName());
			ps.setString(2, foodTypes.getTypeId());
			rs = ps.executeQuery();

			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

	@Override
	public boolean delete(String typeId) {
		boolean result = false;

		String sql = "delete from food_types where type_id=?";

		Connection conn = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, typeId);
			rs = ps.executeQuery();

			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.free(conn, ps, rs);
		return result;
	}

}
